<?php
// SomeryC, a webcomic script by Zachary Weston Lewis
// Based on Somery by Robin de Graaf
// Copyright 2005-2006
// SomeryC is distributed under the Artistic License (see LICENSE.txt)
//
// ADMIN/SYSTEM/ENGINE.PHP > 31-08-2007

include("config.php");
include("admin/system/error.php");
include("admin/system/functions.php");

if ($comments == "post")
{
	if (!$random && !err)
	$err = 1;

	// Begin CAPTCHA comment authentication.
	switch(strtolower($randEntry))
	{
		case "p5wa":
			$newRand = 1;
			break;
		case "5wav":
			$newRand = 2;
			break;
		case "wav2":
			$newRand = 3;
			break;
		case "av2m":
			$newRand = 4;
			break;
		case "v2mi":
			$newRand = 5;
			break;
		case "2mi9":
			$newRand = 6;
			break;
		case "mi9x":
			$newRand = 7;
			break;
		case "i9xs":
			$newRand = 8;
			break;
		case "9xsz":
			$newRand = 9;
			break;
		default:
			$newRand = 127;
			break;
	}

	if ($random != $newRand)
	{
		$err = 1;
		echo "<div style=\"background: #F4C500; border-bottom: 2px solid #000; padding-top: 3px;\"><p style=\"margin: 3px; font-weight: bold; color: #CE0000;\">You entered " . $randEntry . ", which was incorrect.</p></div>";
	}
	// End CAPTCHA comment authentication.

	if (!$author && !$err)
	{
		echo $error[15];
		$err = 1;
	}

	if (!$comment && !$err)
	{
		echo $error[16];
		$err = 1;
	}

	if (!$err)
	{

		// Get the user's IP.
		$ip = $_SERVER['REMOTE_ADDR'];

		// Store the comment into the database.
		$query = "INSERT INTO ".$prefix."comments (parentid,author,time,email,url,comment,ip) VALUES
		('$p','$author',NOW(),'$email','$url','$comment','$ip')";
 		$result = mysql_query($query);

 		// Comment stored. Send an e-mail alert to users.
 		$query = "SELECT * FROM ".$prefix."profile WHERE getmail = '1'";
 		$result = mysql_query($query);

 		while($row = mysql_fetch_object($result))
 		{
			mail("$row->email", "New comment posted on ".$website."!",
			"Name: ".$author."\nEmail: ".$email."\nURL: ".$url."\nIP: ".$ip."\nComment: ".$comment."\nComment URL: ".$websiteurl."/?p=".$p."&c=1",
			"From: SomeryC Admin");
		}

 		include("admin/cookies.php");
		destroy_cookie("mobscommenter");
		destroy_cookie("mobscommentemail");
		destroy_cookie("mobscommenturl");
		setcookie("mobscommenter",$author, time()+30000000);
		setcookie("mobscommentemail",$email, time()+30000000);
		setcookie("mobscommenturl",$url, time()+30000000);
		echo "<meta http-equiv=Refresh content=0;URL=$PHP_SELF?p=$p>";
	}
}

loadsettings();

$update = "SELECT * FROM ".$prefix."comics WHERE status = '0'";
$update_result = mysql_query($update);

while( $row = mysql_fetch_object( $update_result ) )
{
	$timestamp = time();
	if ( strtotime($row->posttime) )
	{
		$posttimecompare = strtotime($row->posttime);
	}
	else
	{
		$posttimecompare = NULL;
	}
	if ( !$posttimecompare || $posttimecompare < $timestamp ) {
		$newStatus = 1;
		$updateQuery = "UPDATE ".$prefix."comics SET status = '$newStatus' WHERE id = '$row->id'";
		mysql_query($updateQuery) or die('Error, query failed');
	}
}

$build = " WHERE status = '1'";
if ($p != "")
	$build .= " AND id = '$p'";
if ($cat)
{
	if (!$p)
	{
		if ($cat)
		{
			$build .= " AND category = '$cat'";
		}
	}
}
if ($offset)
{
	$buildpp = " LIMIT $offset,".$settings[noposts];
}
else
{
	$buildpp = " LIMIT ".$settings[noposts];
}
$query = "SELECT * FROM ".$prefix."comics".$build." ORDER BY id DESC".$buildpp; $result = mysql_query($query);
?>
